<template>
    <div class="add-box">
        <div>
            <el-icon>
                <Tickets />
            </el-icon>
            <span>菜单列表</span>
        </div>
        <div>
            <el-button type="primary" @click="addDataFn">添加</el-button>
        </div>
    </div>
    <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="编号" width="60" header-align="center" align="center" />
        <el-table-column prop="title" label="菜单名称" header-align="center" align="center" />
        <el-table-column prop="url" label="菜单级数" header-align="center" align="center">
            <template #default="scope">
                {{ scope.row.parentId === 0 ? "一级" : "二级" }}
            </template>
        </el-table-column>
        <el-table-column prop="name" label="前端名称" header-align="center" align="center" />
        <el-table-column prop="description" label="前端图标" header-align="center" align="center">
            <template #default="scope">
                <i class="iconfont" :class="scope.row.icon" style="color: black !important;"></i>
            </template>
        </el-table-column>
        <el-table-column label="是否显示" header-align="center" align="center">
            <template #default="scope">
                <el-switch v-model="scope.row.hidden" :active-value="0" :inactive-value="1"
                    @change="switchFn(scope.row)" />
            </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" header-align="center" align="center" />
        <el-table-column label="设置" width="120" header-align="center" align="center">
            <template #default="scope">
                <el-button type='primary' link @click="returnFn" v-show="disabled" :disabled="!disabled">
                    返回上级</el-button>
                <el-button type='primary' style="margin-right:12px ;" link @click="seeChildFn(scope.row.id)"
                    :disabled="disabled">查看下级</el-button>
            </template>
        </el-table-column>
        <el-table-column label="操作" width="120" header-align="center" align="center">
            <template #default="scope">
                <el-button type='primary' link @click="editFn(scope.row.id)">编辑</el-button>
                <el-button type='primary' link @click="deleteFn(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <div class="box">
        <ElConfigProvider :locale="zhCn">
            <el-pagination v-model:currentPage="pageNum" v-model:page-size="pageSize" background
                layout="total, sizes, prev, pager, next ,jumper" :total="total" @size-change="pageSizeChange"
                @current-change="pageNumChange" :page-sizes="[10, 20, 50, 100]" />
        </ElConfigProvider>
    </div>
</template>
<script setup lang='ts'>
import { Tickets } from "@element-plus/icons-vue";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ElConfigProvider, ElMessage, ElMessageBox, FormInstance, FormRules } from "element-plus";
import { reactive, ref } from "vue";
import { user } from "@/api";
import { useTable } from "@/hooks/useTable";
import { useRouter } from "vue-router"
const query = reactive({
    // keyword: "",
    parentId: 0,
    pageSize: 10,
    pageNum: 1,
    total: 0
});
const router = useRouter()
const { tableData, pageNum, pageSize, parentId, total, search, keyword, emptyFn, fetchData } = useTable(user.menuListAPI)
const disabled = ref(false)
const addDataFn = () => {
    router.push("addMenu")

}
const deleteFn = (id: number) => {
    ElMessageBox.confirm('确定删除吗？')
        .then(() => {
            user.deleteMenuAPI(id).then(res => {
                console.log(res);
                fetchData()
            })
        })
        .catch(() => {
            return
        })

}
const seeChildFn = (id: number) => {
    disabled.value = true
    parentId.value = id
    fetchData()
}
const returnFn = () => {
    disabled.value = false
    parentId.value = 0
    fetchData()
}
const pageNumChange = () => {
    fetchData()
}
const pageSizeChange = () => {
    fetchData()
}

const switchFn = (row: IBackgroundReturn) => {
    console.log(row.id, row.hidden);
    // console.log(row);
    user.UpdateHiddenAPI(row.id as number, row.hidden).then(res => {
        console.log(res);
        ElMessage({
            message: res.message,
            type: res.code === 200 ? "success" : "error",
        });

    })


}
const editFn = (id: number) => {
    router.push({ path: "addMenu", query: { id } })
    // user.obtainUserAPI(id).then(res => {
    //     router.push("addMenu")
    //     console.log(res);

    // })
}

</script>
<style lang="less" scoped>
.search-box {
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 10px;

    .flex {
        display: flex;
        justify-content: space-between;
    }
}

.add-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    box-sizing: border-box;
    margin: 20px 0;
    border-radius: 10px;
    // box-shadow: 0 0 4px 2px #ccc;
    border: 1px solid #ccc;

    .el-icon {
        top: 2px;
    }
}

.box {
    display: flex;
    justify-content: flex-end;
    margin: 20px 0;
}
</style>